package org.microblog.service.impl;

import org.microblog.dao.CollectionDao;
import org.microblog.dao.LikesDao;
import org.microblog.dao.impl.CollectionDaoImpl;
import org.microblog.dao.impl.LikesDaoImpl;
import org.microblog.entity.Blog;
import org.microblog.entity.Likes;
import org.microblog.service.LikesService;
import org.microblog.util.DatabaseUtil;
import org.microblog.util.Page;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class LikesServiceImpl implements LikesService {
    @Override
    public int getLikeCountByBid(int bid) throws SQLException {
        Connection conn = null;
        try{
            conn  = DatabaseUtil.getConnection();
            return new LikesDaoImpl(conn).getLikeCountByBid(bid);
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            DatabaseUtil.closeAll(conn,null,null);
        }
    }

    @Override
    public int deleteLikesByBid(int bid) throws SQLException {
        Connection conn = null;
        int result;
        try {
            conn = DatabaseUtil.getConnection();
            conn.setAutoCommit(false);

            LikesDao likesDao = new LikesDaoImpl(conn);
            result = likesDao.deleteLikesByBid(bid);

            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            if (conn != null)
                try {
                    conn.rollback();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            throw e;
        } finally {
            DatabaseUtil.closeAll(conn, null, null);
        }
        return result;
    }

    @Override
    public void getLikeBlogsByPage(int uid, String keyword, Page pageObj) throws SQLException {
        Connection conn = null;
        try{
            conn = DatabaseUtil.getConnection();
            LikesDao likesDao = new LikesDaoImpl(conn);
            int totalCount = likesDao.getLikesCountByUidAndKeyword(uid,keyword);
            pageObj.setTotalCount(totalCount); // 设置总数量并计算总页数
            if (totalCount > 0) {
                // 对末页进行控制
                if (pageObj.getCurrPageNo() > pageObj.getTotalPageCount())
                    pageObj.setCurrPageNo(pageObj.getTotalPageCount());
                // 进行分页查询
                List<Blog> blogList = likesDao.getLikeBlogsByPage(uid,keyword,pageObj.getCurrPageNo(),pageObj.getPageSize());
                pageObj.setBlogsList(blogList);
            } else {
                pageObj.setCurrPageNo(0);
                pageObj.setBlogsList(new ArrayList<Blog>());
            }
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            DatabaseUtil.closeAll(conn,null,null);
        }
    }

    @Override
    public int addLikes(Likes likes) throws SQLException {
        Connection conn = null;
        int result;
        try {
            conn = DatabaseUtil.getConnection();
            conn.setAutoCommit(false);

            LikesDao likesDao = new LikesDaoImpl(conn);
            if(likesDao.findLikes(likes.getUid(),likes.getBid())==null){
                result = likesDao.addLikes(likes);
            }else{
                result = -1;
            }
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            if (conn != null)
                try {
                    conn.rollback();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            throw e;
        } finally {
            DatabaseUtil.closeAll(conn, null, null);
        }
        return result;
    }

    @Override
    public Likes findLikes(int uid, int bid) throws SQLException {
        Connection conn = null;
        try{
            conn  = DatabaseUtil.getConnection();
            return new LikesDaoImpl(conn).findLikes(uid, bid);
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            DatabaseUtil.closeAll(conn,null,null);
        }
    }

    @Override
    public int deleteLikes(Likes likes) throws SQLException {
        Connection conn = null;
        int result;
        try {
            conn = DatabaseUtil.getConnection();
            conn.setAutoCommit(false);

            LikesDao likesDao = new LikesDaoImpl(conn);
            if(likesDao.findLikes(likes.getUid(),likes.getBid())!=null){
                result = likesDao.deleteLikes(likes);
            }else{
                result = -1;
            }
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            if (conn != null)
                try {
                    conn.rollback();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            throw e;
        } finally {
            DatabaseUtil.closeAll(conn, null, null);
        }
        return result;
    }
}
